package com.lgx.right.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.exception.DataException;
import com.lgx.common.tree.easyui.TreeModel;
import com.lgx.common.web.json.JsonUtils;
import com.lgx.common.web.util.DictUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.right.entity.Permission;
import com.lgx.right.service.IPermissionService;

@Controller
@RequestMapping("/permission")
public class PermissionController {

	@Autowired
	private IPermissionService permissionService;
	
	public IPermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}
	
	@RequestMapping("/permissionMgr.do")
	public String permissionMgr(HttpServletRequest request,HttpServletResponse response)
	{
		return "right/permission/permissionMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response)
	{
		List<Permission> ps = permissionService.findPermissionList();
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(ps);
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/init.do")
	public String init(HttpServletRequest request,HttpServletResponse response)
	{
		String type = request.getParameter("type");
		String permissionId = request.getParameter("permissionId");
		if(type.equals("edit")){
			Permission permission = this.permissionService.findPermissionById(Long.parseLong(permissionId));
			request.setAttribute("permission", permission);
		}
		
		//权限类型
		List<Dictionary> permissionTypes = DictUtils.getDictListByKind(DictUtils.PERMISSION_TYPE);
		request.setAttribute("permissionTypes", permissionTypes);
		
		//是否显示
		List<Dictionary> showTypes = DictUtils.getDictListByKind(DictUtils.SHOW_TYPE);
		request.setAttribute("showTypes", showTypes);
		
		request.setAttribute("type", type);
		request.setAttribute("permissionId", permissionId);
		
		return "right/permission/permission-init";
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response)
	{
		String permissionId = request.getParameter("permissionId");   //接受权限id
		Permission parent = this.permissionService.findPermissionById(Long.parseLong(permissionId));
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String type = request.getParameter("type");                   //接受操作类型
		String isDelete = request.getParameter("isDelete");           //接受权限是否可用
		
		Permission p = new Permission();
		p.setName(name);
		p.setParent(parent);
		p.setType(type);
		p.setIsDelete(isDelete);
		int level = Integer.parseInt(parent.getLevel())+1;
		p.setLevel(level+"");
		
		try {
			this.permissionService.createPermission(p);
		} catch (DataException e1) {
			e1.printStackTrace();
			return "0";
		}
		
		request.setAttribute("message", "操作成功！");
		return "1";
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response)
	{
		String permissionId = request.getParameter("permissionId");
		Permission p = this.permissionService.findPermissionById(Long.parseLong(permissionId));
		
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String type = request.getParameter("type");
		String isDelete = request.getParameter("isDelete");
		String layerOrder = request.getParameter("layerOrder");
		
		p.setName(name);
		p.setType(type);
		p.setIsDelete(isDelete);
		p.setLayerOrder(layerOrder);
		
		try {
			this.permissionService.updatePermission(p);
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		
		request.setAttribute("message", "操作成功！");
		return "1";
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response)
	{
		String permissionId = request.getParameter("permissionId");
		try {
			this.permissionService.deletePermission(Long.parseLong(permissionId));
			return "1";
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		return "0";
	}
	
	@RequestMapping("/tree.do")
	public void tree(HttpServletRequest request,HttpServletResponse response)
	{
		List<TreeModel> tm = permissionService.findPermissionTreeList();
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(tm);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//生成权限树状列表
	@RequestMapping("/roleTree.do")
	public void roleTree(HttpServletRequest request,HttpServletResponse response)
	{
		//接受角色id
		String roleId = request.getParameter("roleId");
		
		if(StringUtils.isEmpty(roleId))roleId = "0";
		List<TreeModel> tm = permissionService.findPermissionRoleTreeList(Long.parseLong(roleId));
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(tm);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
